package cn.lovemmd.model.mapper;

import cn.lovemmd.model.pojo.ModelList;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface ModelListMapper extends BaseMapper<ModelList> {
    
    @Select("SELECT * FROM model_list WHERE m_occupation = #{occupation} AND deleted = 0 ORDER BY m_download_count DESC")
    List<ModelList> selectByOccupation(String occupation);
    
    @Select("SELECT * FROM model_list WHERE m_category = #{category} AND deleted = 0 ORDER BY m_download_count DESC")
    List<ModelList> selectByCategory(String category);
    
    @Select("SELECT * FROM model_list WHERE deleted = 0 ORDER BY m_download_count DESC LIMIT #{limit}")
    List<ModelList> selectTopModels(@Param("limit") int limit);
    
    @Select("SELECT * FROM model_list WHERE m_name LIKE CONCAT('%', #{keyword}, '%') AND deleted = 0 ORDER BY m_download_count DESC")
    List<ModelList> searchByKeyword(String keyword);
    
    @Select("SELECT COUNT(*) FROM model_list WHERE deleted = 0")
    Long countTotalModels();
    
    @Select("SELECT m_occupation, COUNT(*) as count FROM model_list WHERE deleted = 0 GROUP BY m_occupation")
    List<Map<String, Object>> getOccupationStats();
    
    @Select("SELECT m_category, COUNT(*) as count FROM model_list WHERE deleted = 0 GROUP BY m_category")
    List<Map<String, Object>> getCategoryStats();
    
    @Update("UPDATE model_list SET m_download_count = m_download_count + 1 WHERE m_id = #{modelId}")
    int incrementDownloadCount(@Param("modelId") Integer modelId);
    
    @Update("UPDATE model_list SET m_view_count = m_view_count + 1 WHERE m_id = #{modelId}")
    int incrementViewCount(@Param("modelId") Integer modelId);
}
